<!-- 新建供应商 -->
<template>
  <a-drawer v-model:visible="vdata.visible" :maskClosable="false" :title="vdata.isAdd ? '新增内部商品' : '修改内部商品'" width="40%">
    <a-form
      ref="formRef"
      :model="vdata.saveObject"
      layout="vertical"
      labelAlign="left"
      :rules="vdata.rules"
      :colon="false"
      :wrapper-col="{ span: 24 }"
      style="margin-top: 30px; margin-left: 20px"
    >
      <a-row>
        <a-col :span="11">
          <a-form-item label="商品名称" name="goodsName">
            <a-input v-model:value="vdata.saveObject['goodsName']" placeholder="供应商名称" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="商品详情" name="goodsDetail" :wrapper-col="{ span: 23 }">
            <a-textarea v-model:value="vdata.saveObject['goodsDetail']" :placeholder="vdata.saveObject['authConfigPlaceholder']" :rows="4" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="商品图片--banner" name="goodsImgBanner" :wrapper-col="{span: 23}">
            <GzUpload v-model:src="vdata.saveObject['goodsImgBanner']" bizType="activity" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="商品图片--列表小图" name="goodsImgListSmall" :wrapper-col="{span: 23}">
            <GzUpload v-model:src="vdata.saveObject['goodsImgListSmall']" bizType="activity" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="商品图片--列表大图" name="goodsImgListBig" :wrapper-col="{span: 23}">
            <GzUpload v-model:src="vdata.saveObject['goodsImgListBig']" bizType="activity" />
          </a-form-item>
        </a-col>
        <a-col :span="11">
          <a-form-item label="市场价" name="marketPrice">
            <a-input v-model:value="vdata.saveObject['marketPrice']" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="备注" name="remark" :wrapper-col="{ span: 23 }">
            <a-textarea v-model:value="vdata.saveObject['remark']" :placeholder="vdata.saveObject['authConfigPlaceholder']" :rows="4" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <div class="drawer-btn-center">
      <a-button :style="{ marginRight: '8px' }" style="margin-right: 8px" @click="onClose">
        <close-outlined />
        取消
      </a-button>
      <a-button type="primary" :loading="vdata.btnLoading" @click="handleOkFunc">
        <check-outlined />
        保存
      </a-button>
    </div>
  </a-drawer>
</template>
  
  <script setup lang="ts">
  // import { req,API_URL_VIRTUAGOODS_SUPPLIER_ADD,API_URL_VIRTUAGOODS_INSIDE_UPDATE } from '@/api/manage'
  import {$apiAwardVirtualSupplierAdd} from '@/api/externalGoods/supplier/index'
  import {$apiVirtualGoodsInsideUpdate} from '@/api/externalGoods/innertable/index'
  import { reactive, ref, defineExpose, getCurrentInstance } from 'vue'
  const { $infoBox, $access } = getCurrentInstance()!.appContext.config.globalProperties
  const formRef = ref()
  
  const props = defineProps(['callBackFunc'])
  
  const vdata:any = reactive({
    isAdd: false, // 新增OR修改
    visible: false,
    btnLoading: true,
    saveObject: {},
    rules: {
        goodsName: [{ required: true, message: '商品名称不能为空', trigger: 'blur' },],
        // goodsDetail: [{ required: true, message: "", trigger: "blur" },],
        goodsImgBanner: [{ required: true, message: '商品图片--banner不能为空', trigger: 'change' },],
        goodsImgListSmall: [{ required: true, message: '商品图片--列表小图不能为空', trigger: 'change' },],
        goodsImgListBig: [{ required: true, message: '商品图片--列表大图', trigger: 'change' },],
        marketPrice: [{ required: true, message: '市场价格不能为空', trigger: 'blur' },],
    },
  })
  
  function handleOkFunc() {
    formRef.value.validate().then(() => {
      vdata.btnLoading = true
      if(vdata.isAdd){  // 新增
        
        $apiAwardVirtualSupplierAdd(vdata.saveObject).then(res=>{
          $infoBox.message.success('新增成功！')
          props.callBackFunc()
          onClose()
        }).finally(()=>{
          vdata.btnLoading = false
        })
      }else{  // 修改
        // vdata.saveObject.marketPrice = vdata.saveObject.marketPrice*100
        $apiVirtualGoodsInsideUpdate({...vdata.saveObject,marketPrice:vdata.saveObject.marketPrice*100}).then(res=>{
          $infoBox.message.success('修改成功！')
          props.callBackFunc()
          onClose()
        }).finally(()=>{
          vdata.btnLoading = false
        })
      }
    }).catch(() => {})
  }
  
  function onClose() {
    vdata.visible = false
  }
  
  function show(record) {    
    vdata.isAdd = !record
    vdata.btnLoading = false
    vdata.saveObject = {}
    if (formRef.value !== undefined) {
      formRef.value.resetFields()
    }
    if(!vdata.isAdd){ // 修改
      vdata.saveObject = { ...record ,marketPrice: record.marketPrice/100,goodsPrice: record.goodsPrice/100}
    }
    vdata.visible = true
  }
  
  defineExpose({
    show,
  })
  </script>